package com.yanchangapp.widget;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.zxl.baselib.util.window.DisplayHelper;
import com.yanchangapp.R;
import com.yanchangapp.model.bean.MsgTypeBean;
import java.util.ArrayList;
import java.util.List;
public class MsgPopupWindow extends PopupWindow {
    private Context context;
    private View window;
    private Animation animationIn, animationOut;
    private boolean isDismiss = false;
    private LinearLayout id_ll_root;
    private BaseQuickAdapter<MsgTypeBean,BaseViewHolder> adapter;
    private List<MsgTypeBean> mList = new ArrayList<>();
    private ImageView mArrowImage;
    private RecyclerView recyclerView;
    private OnPopItemSelectedListener mOnPopItemSelectedListener;
    public MsgPopupWindow(Context context, List<MsgTypeBean> mList) {
        super(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        this.context = context;
        this.mList = mList;
        window = LayoutInflater.from(context).inflate(R.layout.popup_msg_type_list, null);
        this.setContentView(window);
        this.setAnimationStyle(R.style.WindowStyle);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        this.setBackgroundDrawable(new ColorDrawable(Color.argb(123, 0, 0, 0)));
        /* 获得图片*/
        animationIn = AnimationUtils.loadAnimation(context, R.anim.photo_album_show);
        animationOut = AnimationUtils.loadAnimation(context, R.anim.photo_album_dismiss);
        initView();
    }

    public void setImageView(ImageView imageView) {
        this.mArrowImage = imageView;
    }

    private void initView() {
        id_ll_root = (LinearLayout) window.findViewById(R.id.flPopupMsg);
        adapter = new BaseQuickAdapter<MsgTypeBean, BaseViewHolder>(R.layout.item_msg_type,mList) {
            @Override
            protected void convert(BaseViewHolder helper, MsgTypeBean item) {
                TextView tvDes = helper.getView(R.id.tvMsgDes);
                tvDes.setSelected(item.isSelected());
                helper.setText(R.id.tvMsgDes,item.getMsgDescription());
            }
        };

        adapter.setOnItemClickListener((adapter, view, position) -> {
            for (MsgTypeBean bean:mList){
                bean.setSelected(false);
            }
            mList.get(position).setSelected(true);
            adapter.notifyDataSetChanged();
            if (mOnPopItemSelectedListener != null){
                mOnPopItemSelectedListener.popItemSelected(mList.get(position));
            }
        });

        recyclerView = window.findViewById(R.id.recycleMsgType);
        recyclerView.addItemDecoration(new RecycleViewDivider(
                context, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(context,0.5f), ContextCompat.getColor(context, R.color.borderColorBase)));
        recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(adapter);
        id_ll_root.setOnClickListener(v -> MsgPopupWindow.this.dismiss());
    }

    @Override
    public void showAsDropDown(View anchor) {
        try {
            if (Build.VERSION.SDK_INT >= 24) {
                Rect rect = new Rect();
                anchor.getGlobalVisibleRect(rect);
                // 适配全面屏....
                // 解决全面屏手机使用顶部弹窗带来的问题
                if (DisplayHelper.isNavigationBarExist((Activity) context)){
                    int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
                    setHeight(h);
                }else {
                    //全面屏手机需要获取真实高度....
                    int h = DisplayHelper.getRealScreenSize(context)[1] - rect.bottom;
                    setHeight(h);
                }
            }
            super.showAsDropDown(anchor);
            isDismiss = false;
            id_ll_root.startAnimation(animationIn);
            // 动画旋转....
            ObjectAnimator.ofFloat(mArrowImage, View.ROTATION.getName(), 180, 0).start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dismiss() {
        if (isDismiss) {
            return;
        }

        ObjectAnimator.ofFloat(mArrowImage, View.ROTATION.getName(), -180, 0).start();
        isDismiss = true;
        id_ll_root.startAnimation(animationOut);
        animationOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isDismiss = false;
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
                    dismiss4Pop();
                } else {
                    MsgPopupWindow.super.dismiss();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 在android4.1.1和4.1.2版本关闭PopWindow
     */
    private void dismiss4Pop() {
        new Handler().post(MsgPopupWindow.super::dismiss);
    }

    public interface OnPopItemSelectedListener{
        void popItemSelected(MsgTypeBean bean);
    }

    public void setOnItemSelectedListener(OnPopItemSelectedListener onItemSelectedListener){
        this.mOnPopItemSelectedListener = onItemSelectedListener;
    }
}
